﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Logging;
using System.Configuration;
using MMITIntegration.Helpers;
using MMITIntegration.MobipayStagingService;

namespace MMITIntegration
{
    public class MobiPayIntegration
    {
        IntegrationResponse ir;
        MTIXCode_ExternalRedeemWS MTIXAPI;
        RemitXFinancialStagingAPI RemitXAPI;
        MobipayOutput mo;
        SynkronLogger Log;
        Dictionary<string, object> d;
       

        
        public MobiPayIntegration()
        {
            Log  = new SynkronLogger("MMITCore");
            d = new Dictionary<string, object>();
            MTIXAPI = new MobipayStagingService.MTIXCode_ExternalRedeemWS();
            ir = new IntegrationResponse();
            RemitXAPI = new RemitXFinancialStagingAPI();
        }

        public IntegrationResponse RedeemMobiPay(string MTIXCode, long Amount, string Currency, string MobileNo)
        {
            try
            {
                //possibility of mobipay's throwing an exception
                mo = MobipayOutput.FromString(MTIXAPI.redeemMTIXCode("MTIX", Sha256EncryptHelper.Encrypt("MTIX"), MTIXCode, Amount*100, true, Currency, MobileNo));
                d.Clear();
                d.Add(mo.Code, Log.Desc(mo));
                Log.WriteInformation(675643332, "MobipayMTIX Response", d);
                ir.errDescription = mo.Details;
                ir.txtDescription = mo.Details;
                ir.TransactionCode = mo.Result;
                ir.IntegrationParty = IntegrationParty.Mobipay;
                ir.IntegrationTransactionCode = mo.Code + " - " + DateTime.Now.Ticks.ToString();
            }
            catch(Exception Ex){
                d.Add("Exception Occurred", Ex);
                Log.WriteCritical(4000, "Exception Occurred while requesting mobipay redeemvoucher service", Ex);
                //ToDo: Assign relevant information to ir properties
                ir.errDescription = string.Empty;
                ir.txtDescription = string.Empty;
                ir.TransactionCode = string.Empty;
                ir.IntegrationParty = IntegrationParty.Mobipay;
                ir.IntegrationTransactionCode = string.Empty;
            }
            
            return ir;
        }

        public IntegrationResponse SendMoneyToMobiPayCustomer(string RecipientName, string recipientMSISDN, string senderName, string senderMSISDN, decimal? Amount, string TransactionCode)
        {
            try
            {
                
                rxWsResponse resp = RemitXAPI.GetSessionID("mmit_staging", "81dc9bdb52d04dc20036dbd8313ed055", "MMIT");
                
                d.Clear();
                d.Add("2", Log.Desc(resp ));
                Log.WriteInformation(67564334, "Mobipay RemitX Session Response", d);

              
                resp = RemitXAPI.CreateRemittance(resp.serviceSessionId, TransactionCode, DateTime.Now.ToString("yyyy-MM-dd"), (double)Amount, 0, senderName, senderMSISDN, "", "", "", "KE", RecipientName, recipientMSISDN, "", "", "");
              

                d.Clear();
                d.Add("3", Log.Desc(resp));
                Log.WriteInformation(67564335, "Mobipay RemitX Create Remittance Response", d);

                ir.IntegrationParty = IntegrationParty.Mobipay;
                ir.txtDescription = resp.errorMessage;
                ir.IntegrationTransactionCode = resp.errorCode.ToString ();
                
            }
            catch (Exception ex)
            { 
                d.Add("Exception Occurred", ex);
                Log.WriteCritical(4001, "Exception Occurred while requesting mobipay's remitX service", ex);
                //ToDo: Assign relevant information to ir properties
                ir.errDescription = string.Empty;
                ir.txtDescription = string.Empty;
                ir.TransactionCode = string.Empty;
                ir.IntegrationParty = IntegrationParty.Mobipay;
                ir.IntegrationTransactionCode = string.Empty;                
            }
            return ir;
        }
        
    }
}